package com.dbgs.insurance.insuredperson.view.city;

import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatTextView;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;

import com.dbgs.insurance.insuredperson.R;
import com.dbgs.insurance.insuredperson.activity.register.RegisterInfoActivity;
import com.dbgs.insurance.insuredperson.utils.TextUntil;
import com.dbgs.insurance.insuredperson.view.wheeldata.OnWheelChangedListener;
import com.dbgs.insurance.insuredperson.view.wheeldata.WheelView;
import com.dbgs.insurance.insuredperson.view.wheeldata.WheelViewActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class CitySelectedActivity extends WheelViewActivity implements OnClickListener, OnWheelChangedListener {

    @BindView(R.id.null_view)
    View nullView;
    @BindView(R.id.id_province)
    WheelView idProvince;
    @BindView(R.id.id_city)
    WheelView idCity;
    @BindView(R.id.id_district)
    WheelView idDistrict;
    @BindView(R.id.tv_cancel)
    AppCompatTextView tvCancel;
    @BindView(R.id.tv_ok)
    AppCompatTextView tvOk;

    private int requestCode = 2;
    private String city;
    private int currentProvice, currentCity, currentDistrict;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city_selected);
        ButterKnife.bind(this);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = LayoutParams.FILL_PARENT;
        lp.height = LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(lp);
        setUpListener();
        setUpData();
    }

    private void setUpListener() {
        idProvince.addChangingListener(this);
        idCity.addChangingListener(this);
        idDistrict.addChangingListener(this);
    }

    private void setUpData() {
        initProvinceDatas();
        idProvince.setViewAdapter(new ArrayWheelAdapter<String>(CitySelectedActivity.this, mProvinceDatas));
        updateCities();
        updateAreas();
        if (TextUntil.isValidate(getIntent().getIntExtra("currentProvice", 0) + "")) {
            idProvince.setCurrentItem(getIntent().getIntExtra("currentProvice", 0));
            // 省
            mCurrentProviceName = mProvinceDatas[getIntent().getIntExtra("currentProvice", 0)];
            String[] cities = mCitisDatasMap.get(mCurrentProviceName);
            if (cities == null) {
                cities = new String[]{""};
            }
            idCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
            // 市
            idCity.setCurrentItem(getIntent().getIntExtra("currentCity", 0));

            mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[getIntent().getIntExtra("currentCity", 0)];
            String[] areas = mDistrictDatasMap.get(mCurrentCityName);

            if (areas == null) {
                areas = new String[]{""};
            }
            idDistrict.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));
            // 区
            idDistrict.setCurrentItem(getIntent().getIntExtra("currentCity", 0));
        }
        idProvince.setVisibleItems(5);
        idCity.setVisibleItems(5);
        idDistrict.setVisibleItems(5);

    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        // TODO Auto-generated method stub
        if (wheel == idProvince) {
            updateCities();
            mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[0];
            mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
        } else if (wheel == idCity) {
            updateAreas();
            mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[0];
            mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
        } else if (wheel == idDistrict) {
            mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];
            mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
        }
    }

    private void updateAreas() {
        int pCurrent = idCity.getCurrentItem();
        mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
        String[] areas = mDistrictDatasMap.get(mCurrentCityName);

        if (areas == null) {
            areas = new String[]{""};
        }
        idDistrict.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));
        idDistrict.setCurrentItem(0);
    }

    private void updateCities() {
        int pCurrent = idProvince.getCurrentItem();
        mCurrentProviceName = mProvinceDatas[pCurrent];
        String[] cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new String[]{""};
        }
        idCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
        idCity.setCurrentItem(0);
        updateAreas();
    }

    private void showSelectedResult() {
        city = mCurrentProviceName +"-"+ mCurrentCityName +"-"+ mCurrentDistrictName;
        // private String currentProvice,currentCity,currentDistrict;
        currentProvice = idProvince.getCurrentItem();
        currentCity = idCity.getCurrentItem();
        currentDistrict = idDistrict.getCurrentItem();
    }

    @OnClick({R.id.null_view, R.id.tv_cancel, R.id.tv_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.null_view:
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_ok:
                showSelectedResult();
                Intent mIntent = new Intent();
                mIntent.putExtra("change01", city);
                mIntent.putExtra("currentProvice", currentProvice);
                mIntent.putExtra("currentCity", currentCity);
                mIntent.putExtra("currentDistrict", currentDistrict);
                // 设置结果，并进行传送
                this.setResult(RegisterInfoActivity.RESULT_OK, mIntent);
                this.finish();
                break;
        }
    }
}
